<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		
		<script src='../../../jsunit/app/jsUnitCore.js' type='text/javascript'></script>
		<script type="text/javascript" src='../javascript/magic_table.js'></script>
		
		<title>Table model JSUnit tests</title>
	</head>
	<body>
		
		<script type='text/javascript'>
		
			var tableModel;
			var rows = 10;
			var columns = 20;
			var numColumnHeaders = 1;
			var numRowHeaders  = 1;
			var defaultRowHeight = 11;
			var defaultColumnWidth = 19;
				
			function setUp()
			{
				tableModel = new greg.ross.visualisation.TableModel(rows, columns, defaultRowHeight, defaultColumnWidth, numRowHeaders, numColumnHeaders);
				
				for (var i = 0; i < rows; i++)
					for (var j = 0; j < columns; j++)
						tableModel.setContentAt(i, j, (i*j));
			}
			
			function testCellDimensions()
			{
				tableModel = new greg.ross.visualisation.TableModel(rows, columns, 4, 19, 1, 1);
				assertEquals(4, tableModel.getRowHeight());
				assertEquals(19, tableModel.getColumnWidth());
				
				tableModel = new greg.ross.visualisation.TableModel(rows, columns, 19, 4, 1, 1);
				assertEquals(19, tableModel.getRowHeight());
				assertEquals(4, tableModel.getColumnWidth());
				
				tableModel = new greg.ross.visualisation.TableModel(rows, columns, 4, 4, 1, 1);
				assertEquals(4, tableModel.getRowHeight());
				assertEquals(4, tableModel.getColumnWidth());
			}
			
			function testModel()
			{
				assertEquals(10, tableModel.rowCount);
				assertEquals(20, tableModel.columnCount);
				assertEquals(11, tableModel.getRowHeight(false));
				assertEquals(19, tableModel.getColumnWidth(false));
				assertNotEquals(null, tableModel.matrix);
			}
		
			function testTableModelMatrix()
			{
				assertEquals(0, tableModel.matrix.get([0, 0]));
				assertEquals(81, tableModel.matrix.get([9, 9]));
				assertEquals(171, tableModel.matrix.get([9, 19]));
				assertEquals(20, tableModel.matrix.get([5, 4]));
			}
			
			function testGetContentAt()
			{
				assertEquals(0, tableModel.getContentAt(0, 0));
				assertEquals(81, tableModel.getContentAt(9, 9));
				assertEquals(171, tableModel.getContentAt(9, 19));
				assertEquals(20, tableModel.getContentAt(5, 4));
			}
			
			function testSetContentAt()
			{
				tableModel.setContentAt(0, 0, "peachy");
				assertEquals("peachy", tableModel.getContentAt(0, 0));
				
				tableModel.setContentAt(0, 1, 20);
				assertEquals(20, tableModel.getContentAt(0, 1));
			}
			
			function testSetContentAtInvalidCells()
			{
				tableModel.setContentAt(1000, 0, "peachy");
				assertEquals(null, tableModel.getContentAt(1000, 0));
				
				tableModel.setContentAt(0, 1000, 20);
				assertEquals(null, tableModel.getContentAt(0, 1000));
			}
			
			function testMinMaxValues()
			{
				var minValue = 10;
				var maxValue = 50;
				var tableModel = new greg.ross.visualisation.TableModel(1, 2, 5, 5, 0, 0);
				tableModel.setContentAt(0, 0, minValue);
				tableModel.setContentAt(0, 1, maxValue);
				
				assertEquals(minValue, tableModel.getMinValue());
				assertEquals(maxValue, tableModel.getMaxValue());
			}
			
			function testMinMaxRowValues()
			{
				var minValue = 10;
				var maxValue = 50;
				var tableModel = new greg.ross.visualisation.TableModel(2, 2, 5, 5, 0, 0);
				tableModel.setContentAt(0, 0, minValue);
				tableModel.setContentAt(0, 1, maxValue);
				tableModel.setContentAt(1, 1, 3);
				
				assertEquals(minValue, tableModel.getMinValueForRow(0));
				assertEquals(maxValue, tableModel.getMaxValueForRow(0));
				assertEquals(3, tableModel.getMinValueForRow(1));
				assertEquals(3, tableModel.getMaxValueForRow(1));
			}
			
			function testMinMaxColumnValues()
			{
				var minValue = 10;
				var maxValue = 50;
				var tableModel = new greg.ross.visualisation.TableModel(2, 2, 5, 5, 0, 0);
				tableModel.setContentAt(0, 0, minValue);
				tableModel.setContentAt(0, 1, maxValue);
				tableModel.setContentAt(1, 1, 3);
				
				assertEquals(minValue, tableModel.getMinValueForColumn(0));
				assertEquals(minValue, tableModel.getMaxValueForColumn(0));
				
				assertEquals(3, tableModel.getMinValueForColumn(1));
				assertEquals(maxValue, tableModel.getMaxValueForColumn(1));
				
				tableModel.setContentAt(0, 1, 100);
				assertEquals(100, tableModel.getMaxValueForColumn(1));
				
				tableModel = new greg.ross.visualisation.TableModel(1, 2, 5, 5, 0, 0);
				tableModel.setContentAt(0, 0, 0);
				tableModel.setContentAt(1, 0, 1);
				tableModel.setContentAt(2, 0, 2);
				assertEquals(0, tableModel.getMinValueForColumn(0));
				
				tableModel.setContentAt(0, 0, -10);
				assertEquals(-10, tableModel.getMinValueForColumn(0));
			}
			
			function testRecalculateMinMaxValues()
			{
				var tableModel = new greg.ross.visualisation.TableModel(2, 2, 5, 5, 0, 0);
				tableModel.setContentAt(0, 0, 0);
				tableModel.setContentAt(1, 0, 1);
				assertEquals(1, tableModel.getMaxValueForColumn(0));
				
				tableModel.setContentAt(1, 0, 0);
				assertEquals(1, tableModel.getMaxValueForColumn(0));
				
				tableModel.recalculateMinMaxValues();
				assertEquals(0, tableModel.getMaxValueForColumn(0));
				
				tableModel.setContentAt(1, 0, -10);
				assertEquals(0, tableModel.getMaxValueForColumn(0));
				
				tableModel.recalculateMinMaxValues();
				assertEquals(0, tableModel.getMaxValueForColumn(0));
				
				tableModel.setContentAt(1, 0, 22);
				tableModel.setContentAt(1, 0, 21);
				assertEquals(22, tableModel.getMaxValueForColumn(0));
				
				tableModel.recalculateMinMaxValues();
				assertEquals(21, tableModel.getMaxValueForColumn(0));
				
				tableModel.setContentAt(0, 0, 21);
				assertEquals(21, tableModel.getMaxValueForRow(0));
				
				tableModel.setContentAt(0, 0, 20);
				assertEquals(21, tableModel.getMaxValueForRow(0));
				
				tableModel.recalculateMinMaxValues();
				assertEquals(20, tableModel.getMaxValueForRow(0));
				
				tableModel.setContentAt(0, 1, 80);
				assertEquals(80, tableModel.getMaxValueForRow(0));
				
				tableModel.setContentAt(0, 1, 79);
				assertEquals(80, tableModel.getMaxValueForRow(0));
				
				tableModel.recalculateMinMaxValues();
				assertEquals(79, tableModel.getMaxValueForRow(0));
			}
			
			function testGetColourGradientForRow()
			{
				tableModel.recalculateMinMaxValues();
				var colourGradient;
				
				for (var i = 0; i < rows; i++) 
				{
					colourGradient = tableModel.getColourGradientForRow(i);
					assertNotEquals(null, colourGradient);
				}
			}
			
			function testMinMaxRowValuesAfterAddingAString()
			{
				var minValue = 10;
				var maxValue = 50;
				var tableModel = new greg.ross.visualisation.TableModel(1, 2, 5, 5, 0, 0);
				tableModel.setContentAt(0, 0, minValue);
				tableModel.setContentAt(0, 1, maxValue);
				
				assertEquals(minValue, tableModel.getMinValueForRow(0));
				assertEquals(maxValue, tableModel.getMaxValueForRow(0));
				
				tableModel.setContentAt(0, 1, "pup");
				assertEquals(minValue, tableModel.getMinValueForRow(0));
				assertEquals(maxValue, tableModel.getMaxValueForRow(0));
			}
			
			function testGetCellRendererAt()
			{
				var renderer = tableModel.getCellRendererAt(0, 0, false);
				assertTrue(renderer instanceof greg.ross.visualisation.ScaleCellRenderer);
				
				var renderer = tableModel.getCellRendererAt(1, 0, false);
				assertTrue(renderer instanceof greg.ross.visualisation.ScaleCellRenderer);
				
				var renderer = tableModel.getCellRendererAt(0, 1, false);
				assertTrue(renderer instanceof greg.ross.visualisation.ScaleCellRenderer);
				
				var renderer = tableModel.getCellRendererAt(0, 0, true);
				assertTrue(renderer instanceof greg.ross.visualisation.FisheyeCellRenderer);
				
				var renderer = tableModel.getCellRendererAt(1, 1, false);
				assertTrue(renderer instanceof greg.ross.visualisation.DefaultCellRenderer);
				
				var renderer = tableModel.getCellRendererAt(1, 1, true);
				assertTrue(renderer instanceof greg.ross.visualisation.FisheyeCellRenderer);
			}
			
			function testGetColumnWidth()
			{
				tableModel.setColumnWidth(0, 101);
				assertEquals(101, tableModel.getColumnWidth(0));
				assertEquals(defaultColumnWidth, tableModel.getColumnWidth('blah'));
				
				tableModel.setColumnWidth(3, 3);
				assertEquals(3, tableModel.getColumnWidth(3));
				assertEquals(defaultColumnWidth, tableModel.getColumnWidth('blah'));
			}
			
			function testGetColumnWidth()
			{
				tableModel.setRowHeight(0, 12);
				assertEquals(12, tableModel.getRowHeight(0));
				assertEquals(defaultRowHeight, tableModel.getRowHeight('blah'));
				
				tableModel.setRowHeight(2, 8);
				assertEquals(8, tableModel.getRowHeight(2));
				assertEquals(defaultRowHeight, tableModel.getRowHeight('blah'));
			}
		
		</script>
		
	</body>
</html>
